package swu.edu.cn.javaee.service;


import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import swu.edu.cn.javaee.entity.*;
import swu.edu.cn.javaee.mapper.MerchantMapper;
import swu.edu.cn.javaee.mapper.UserMapper;

import java.util.ArrayList;
import java.util.List;

import static swu.edu.cn.javaee.component.Signal.*;

@Service
public class MerchantService {

    @Resource
    private MerchantMapper merchantMapper;

    @Resource
    private UserMapper userMapper;

    /**
     * 增加（保存）商户信息
     * @param merchant
     * @return
     */
    public String saveMerchant(Merchant merchant){
        Merchant merchant1= merchantMapper.queryByLicenseNumber(merchant.getUserUid());
        if(merchant1==null){
            merchantMapper.saveMerchant(merchant);
            return SUCCESS_CODE;
        }
        else {
            System.out.println(merchant1);
            return FAILED_CODE_MERCHANT_EXIST;
        }
    }

    /**
     * 根据商户uid查询商户信息
     * @param userUid
     * @return
     */
    public MerchantInfo queryMerchant(String userUid){
        Merchant merchant=merchantMapper.queryMerchant(userUid);
        MerchantInfo merchantInfo=new MerchantInfo();
        if (merchant!=null){
            merchantInfo.setSignal(SUCCESS_CODE);
            merchantInfo.setMerchant(merchant);
            System.out.println(merchant);
        }
        else {
            merchantInfo.setSignal(FAILED_CODE_MERCHANT_EXIST);
        }
        return merchantInfo;
    }

    /**
     * 更新商户信息
     * @param merchant
     * @return
     */
    public String updateMerchant(Merchant merchant){
        Merchant merchant1=merchantMapper.queryMerchant(merchant.getUserUid());
        if(merchant1!=null){
            merchant.setCid(merchant1.getCid());
            merchant.setRegisterName(merchant1.getRegisterName());
            merchant.setLicenseNumber(merchant1.getLicenseNumber());
            System.out.println(merchant);
            merchantMapper.updateMerchant(merchant);
            return SUCCESS_CODE;
        }
        else {
            return FAILED_CODE_MERCHANT_NO_EXIST;
        }
    }

    /**
     * 删除（注销）商户信息
     * @param cid
     * @return
     */
    public String deleteMerchant(String cid){
        System.out.println("删除cid:"+cid);
        Merchant merchant=merchantMapper.queryByCid(cid);
        if(merchant!=null){
            merchantMapper.deleteMerchant(cid);
            userMapper.deleteByUid(merchant.getUserUid());
            return SUCCESS_CODE;
        }
        else {
            return FAILED_CODE_MERCHANT_NO_EXIST;
        }
    }

    /**
     * 查询所有商户信息
     * */
    public MerchantInfoP queryList(){
        List<Merchant> list=merchantMapper.queryList();
        int count=merchantMapper.queryCount();
        MerchantInfoP merchantInfoP=new MerchantInfoP();
        if(list.size()!=0){
            merchantInfoP.setCode("0");
            merchantInfoP.setCount(String.valueOf(count));
            merchantInfoP.setMsg("成功");
            merchantInfoP.setData(list);
            System.out.println(list);
        }
        else {
            merchantInfoP.setCode("1");
            merchantInfoP.setMsg("查询失败");
        }
        return merchantInfoP;
    }

    /**
     * 分页查询处理
     * */
    public MerchantInfoP queryListLimit(String page,String limits){
        Integer start=Integer.valueOf(page);
        Integer limit=Integer.valueOf(limits);
        MerchantInfoP merchantInfoP=new MerchantInfoP();
        int count=merchantMapper.queryCount();
        List<Merchant> list=merchantMapper.queryListLimit((start-1)*limit,limit);
        if(list.size()!=0){
            merchantInfoP.setCode("0");
            merchantInfoP.setCount(String.valueOf(count));
            merchantInfoP.setMsg("成功！");
            merchantInfoP.setData(list);
            System.out.println("分页查询成功!："+limit);
        }else {
            merchantInfoP.setCode("1");
            merchantInfoP.setMsg("失败");
        }
        return merchantInfoP;
    }

    /**
     * 后台根据uid搜索商户
     * */
    public MerchantInfoP queryMerchantP(String userUid){
        MerchantInfoP merchantInfoP=new MerchantInfoP();
        Merchant merchant=merchantMapper.queryMerchant(userUid);
        List<Merchant> list=new ArrayList<Merchant>();
        if(merchant!=null){
            list.add(merchant);
            merchantInfoP.setCode("0");
            merchantInfoP.setCount("1");
            merchantInfoP.setData(list);
            merchantInfoP.setMsg("成功");
            System.out.println(merchant);
        }else {
            merchantInfoP.setCode("1");
            merchantInfoP.setMsg("失败");
        }
        return merchantInfoP;
    }

    /**
     * 后台删除商户
     * */
    public MerchantInfoP deleteMerchantP(String cid){
        Merchant merchant=merchantMapper.queryByCid(cid);
        MerchantInfoP merchantInfoP=new MerchantInfoP();
        if(merchant!=null){
            System.out.println("后台删除cid:"+cid);
            merchantMapper.deleteMerchant(cid);
            userMapper.deleteByUid(merchant.getUserUid());
            merchantInfoP.setCode("0");
            merchantInfoP.setMsg("删除成功");
        }
        else {
            merchantInfoP.setCode("1");
            merchantInfoP.setMsg("失败");
        }
        return merchantInfoP;
    }
}
